import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/news.dart';
import 'package:kxy_flutter_app/view/news/news_detail_view.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';

class NewsItemWidget extends StatefulWidget {
  final News news;
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;
  NewsItemWidget({
    @required this.news,
    this.padding = EdgeInsets.zero,
    this.margin = EdgeInsets.zero,
  });

  @override
  State<StatefulWidget> createState() {
    return new NewsItemWidgetState();
  }
}

class NewsItemWidgetState extends State<NewsItemWidget> {
  @override
  Widget build(BuildContext context) {
    var newsWidget = Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(right: 10),
          child: _createImage(),
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(child: _createTitle()),
              _createDate(),
            ],
          ),
        ),
      ],
    );

    return GestureDetector(
      child: Container(
        height: 90 + widget.padding.vertical,
        color: Colors.white,
        width: MediaQuery.of(context).size.width,
        padding: widget.padding,
        margin: widget.margin,
        child: newsWidget,
      ),
      onTap: () async {
        await Navigator.of(context).push(
          MaterialPageRoute(builder: (context) {
            return NewsDetailView(newsId: widget.news.id);
          }),
        );
        setState(() {
          widget.news.hits++;
        });
      },
    );
  }

  Widget _createImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(5),
      child: Container(
        color: Color.fromRGBO(240, 240, 240, 1),
        child: CachedNetworkImage(
          imageUrl: widget.news.coverUrl,
          width: 160,
          height: 90,
          fit: BoxFit.cover,
          errorWidget: (cxt, str, obj) {
            return Center(
              child: Text('加载出错'),
            );
          },
        ),
      ),
    );
  }

  Widget _createDate() {
    var color = Color.fromRGBO(136, 136, 136, 1);
    var txtStyle = TextStyle(color: color, fontSize: 11);
    var format = new DateFormat('yyyy-MM-dd');
    var date = format.format(widget.news.createTime);
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Icon(
                LineAwesomeIcons.clock_o,
                size: 14,
                color: color,
              ),
              Padding(
                padding: EdgeInsets.only(left: 2),
                child: Text(date, style: txtStyle),
              ),
            ],
          ),
          Row(
            children: [
              Icon(
                LineAwesomeIcons.eye,
                size: 14,
                color: color,
              ),
              Padding(
                padding: EdgeInsets.only(left: 2),
                child: Text(widget.news.hits.toString() + '人阅读', style: txtStyle),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _createTitle() {
    return Text(
      widget.news.title,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        fontSize: 15,
        color: Colors.black87,
      ),
    );
  }
}
